package pl.vanta.utils.commons.managers;

import java.util.MissingResourceException;
import java.util.ResourceBundle;

public class ResourceBundleManager extends AbstractManager<String, String>
{
	private ResourceBundleManager	parent;
	private ResourceBundle			bundle;

	public ResourceBundleManager(ResourceBundle bundle)
	{
		this(bundle, null);
	}

	public ResourceBundleManager(Class<?> clazz)
	{
		this(clazz.getName(), null);
	}

	public ResourceBundleManager(Object o)
	{
		this(o.getClass(), null);
	}

	public ResourceBundleManager(String resource)
	{
		this(ResourceBundle.getBundle(resource), null);
	}

	public ResourceBundleManager(Class<?> clazz, ResourceBundleManager parent)
	{
		this(clazz.getName(), parent);
	}

	public ResourceBundleManager(Object o, ResourceBundleManager parent)
	{
		this(o.getClass(), parent);
	}

	public ResourceBundleManager(String resource, ResourceBundleManager parent)
	{
		this(ResourceBundle.getBundle(resource), parent);
	}

	public ResourceBundleManager(ResourceBundle bundle, ResourceBundleManager parent)
	{
		this.parent = parent;
		this.bundle = bundle;
	}

	@Override
	public String resourceNotFound(String key)
	{
		String value = null;

		try
		{
			value = this.bundle.getString(key);
		}
		catch (MissingResourceException e)
		{
		}

		if (value == null && this.parent != null)
		{
			value = this.parent.get(key);
		}

		if (value == null)
		{
			value = "??? " + key + " ???";
		}

		return value;
	}
}